* This script contains the code to replicate TableE of the on-line appendix in Ductor, L., Fafchamps, M., Goyal S. and M. van der Leij. Social Networks and Research Output. The Review of Economics and Statistics.
/* Including quadratic productivity*/
log using TableE.log, replace
use prodndnp_fullsample.dta

set matsize 6000
set more off


/*******OLS REGRESIONS**************/

/*OLS out-of-sample program*/
cap pro drop ols_os
program define ols_os
syntax varlist(numeric min=1) [if]
marksample touse
reg `varlist' if `touse', vce(cluster auth)
qui scalar n1=e(N)
estat ic
scalar rmseols=e(rmse)
predict ybols if group==2 & lprodf3<., xb 
scalar r2i1ols= e(r2)   /*computing the R2 of the baseline model*/
qui gen sqerrols= (lprodf3-ybols)^2 if ybols<.
quietly egen peols=sum(sqerrols)if ybols<.  /*RSEE of the predicted model*/
quietly sum peols    /*summarizing the RSEE*/
quietly scalar rssols=r(max)  /*obtaining the total RSEE*/
quietly scalar nols=r(N)    /*number of observations in group 2*/
scalar rmseo2ols=(rssols/nols)^0.5   /*RMSE out of sample*/
di n1 " Number of observations in the in-sample group"
di rmseo2ols "  Pooled OLS out-of-sample RMSE"
di nols " Number of observations in the out-of-sample group"
drop peols ybols
end

set more off

/*MODEL 1*/

reg lprodf3 y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 lprodf3lsq-lprodf3l13sq if group==1
qui scalar n1=e(N)
estat ic
scalar rmseols=e(rmse)
predict ybols if group==2 & lprodf3<., xb 
scalar r2i1ols= e(r2)   /*computing the R2 of the baseline model*/
qui gen sqerrolsM1= (lprodf3-ybols)^2 if ybols<.
quietly egen peols=sum(sqerrolsM1)if ybols<.  /*RSEE of the predicted model*/
quietly sum peols    /*summarizing the RSEE*/
quietly scalar rssols=r(max)  /*obtaining the total RSEE*/
quietly scalar nols=r(N)    /*number of observations in group 2*/
scalar rmseo2ols=(rssols/nols)^0.5   /*RMSE out of sample*/
di rmseols   "  Pooled OLS in-sample RMSE"
di n1 " Number of observations in the in-sample group"
di rmseo2ols "  Pooled OLS out-of-sample RMSE"
di nols " Number of observations in the out-of-sample group"
drop peols ybols



/*******MULTIVARIATE MODEL 3***********/

ols_os lprodf3 y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13  lprodf3lsq-lprodf3l13sq lnetprod1y-lnetprod8y lnetprod21y-lnetprod28y degree1y-degree8y degree21y-degree28y gc1y-gc8y lbet1y-lbet8y lclos1y-lclos8y neiq1fs1y-neiq1fs8y if group==1
gen e=1 if e(sample)
/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrolsM1-sqerrols
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(13)
drop sqerrols diffsqerrlM3

/*Netprod*/

ols_os lprodf3 y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 lprodf3lsq-lprodf3l13sq lnetprod1y-lnetprod12y if e==1

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrolsM1-sqerrols
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(13)
drop sqerrols diffsqerrlM3

/*Netprod 2*/


ols_os lprodf3 y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 lprodf3lsq-lprodf3l13sq lnetprod21y-lnetprod211y if e==1

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrolsM1-sqerrols
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(13)
drop sqerrols diffsqerrlM3

/*Degree*/

ols_os lprodf3 y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 lprodf3lsq-lprodf3l13sq degree1y-degree6y if e==1

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrolsM1-sqerrols
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(13)
drop sqerrols diffsqerrlM3


/*degree2*/


ols_os lprodf3 y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 lprodf3lsq-lprodf3l13sq degree21y-degree25y if e==1

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrolsM1-sqerrols
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(13)
drop sqerrols diffsqerrlM3


/*Giant component*/
ols_os lprodf3 y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 lprodf3lsq-lprodf3l13sq gc1y-gc8y if e==1
/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrolsM1-sqerrols
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(13)
drop sqerrols diffsqerrlM3

/*Betweenness*/
ols_os lprodf3 y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 lprodf3lsq-lprodf3l13sq gc1y-gc9y lbet1y-lbet9y if e==1

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrolsM1-sqerrols
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(13)
drop sqerrols diffsqerrlM3


/*Closeness*/

ols_os lprodf3 y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 lprodf3lsq-lprodf3l13sq gc1y-gc7y lclos1y-lclos10y if e==1

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrolsM1-sqerrols
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(13)
drop sqerrols diffsqerrlM3


/* Working with a top 1%*/

ols_os lprodf3 y2-y27 nopapers t2-t27 lprodf3l-lprodf3l13 lprodf3lsq-lprodf3l13sq neiq1fs1y-neiq1fs13y if e==1

/*Diebold-Mariano test*/
gen diffsqerrlM3= sqerrolsM1-sqerrols
newey2 diffsqerrlM3 if group==2 & lprodf3<., lag(13)
drop sqerrols diffsqerrlM3


log close

